from jira import JIRA
class PersonCal():
    def __init__(self,name,works):
        self.name=name
        self.totalTime=0
        self.leaveTime=0
        self.finishTime=0
        for work in works:
            t=work.time
            self.totalTime=self.totalTime+t
            if work.finish:
                self.finishTime=self.finishTime+t
            else:
                self.leaveTime=self.leaveTime+t
    def __str__(self):
        return '姓名:{},总计时间:{}h,剩余时间:{}h,完成时间:{}h'.format(
            self.name.rjust(4),self.totalTime,self.leaveTime,self.finishTime
        );
class PersonWork():
    def __init__(self,name,t,finish):
        self.name=name
        self.time=float(t)
        self.finish=finish=='完成'
def getOutPut():
    jira=JIRA(server="http://10.25.25.117:8080",basic_auth=("195277","dingmiaobin"))
    projectName="XCYBER"
    # boardName="XCYBER 看板"
    # sprintName="xCyber2021-Sprint5"
    project=jira.project(projectName)
    print(project)
    # project.
    sprints=jira.sprints(15)
    sprintName=sprints[len(sprints)-1].name
    jql='project="{}" and sprint="{}"'.format(projectName,sprintName)
    issues=jira.search_issues(jql,fields='',maxResults=999999)
    works= {}
    for i in issues:
        if i.fields.assignee is None:
            print("i:{},assignee:{}".format(i,i.fields.assignee))
        elif i.fields.customfield_10220!=None and i.fields.customfield_10220 !='0.0':
            resolution=i.fields.resolution.name if i.fields.resolution is not None else "未完成"
            name = i.fields.assignee.displayName
            print("key:{key},time:{time},assignee:{assignee},resolution:{resolution}".format(
                key=i.key,time=i.fields.customfield_10220,assignee=name,resolution=resolution
            ))

            if not works.__contains__(name):
                works[name]=[]
            works[name].append(PersonWork(name,i.fields.customfield_10220,resolution))
    cal=[]
    for key in works:
        cal.append(PersonCal(key,works[key]))

    print("-------------统计结果-----------------")
    str=""
    for v in cal:
        str=str+str(v)+"\n"
    return str;
if __name__ == '__main__':
    jira=JIRA(server="http://10.25.25.117:8080",basic_auth=("195277","dingmiaobin"))
    projectName="XCYBER"
    boardName="XCYBER 看板"
    sprintName="xCyber2021-Sprint5"
    project=jira.project(projectName)
    print(project)
    # project.
    sprints=jira.sprints(15)
    sprintName=sprints[len(sprints)-1].name
    jql='project="{}" and sprint="{}"'.format(projectName,sprintName)
    issues=jira.search_issues(jql,fields='',maxResults=999999)
    works= {}
    for i in issues:
        if i.fields.assignee is None:
            print("i:{},assignee:{}".format(i,i.fields.assignee))
        elif i.fields.customfield_10220!=None and i.fields.customfield_10220 !='0.0':
            resolution=i.fields.resolution.name if i.fields.resolution is not None else "未完成"
            name = i.fields.assignee.displayName
            print("key:{key},time:{time},assignee:{assignee},resolution:{resolution}".format(
                key=i.key,time=i.fields.customfield_10220,assignee=name,resolution=resolution
            ))

            if not works.__contains__(name):
                works[name]=[]
            works[name].append(PersonWork(name,i.fields.customfield_10220,resolution))
    cal=[]
    for key in works:
        cal.append(PersonCal(key,works[key]))

    print("-------------统计结果-----------------")
    for v in cal:
        print(v)
        # print(dir(i.fileds))

    # [x.fields.customfield_10220 for x in issues]
    # boards=jira.boards()
    # for board in boards:
    #     if board.name==boardName:
    #         borad_id=board.id
    # print(boards)
    # sprints=jira.sprints(borad_id)
    # for sprint in sprints:
    #     sprintId=sprint.id
    # print()
    #
    # # jira.sprint_info(board_id=borad_id,sprint_id=sprintId)


    #loginParam os_username=195277&os_password=dingmiaobin&os_cookie=true&os_destination=&user_role=&atl_token=&login=%E7%99%BB%E5%BD%95
    #
    # requestBody={
    #     "username":"195277",
    #     "password":"dingmiaobin"
    # }
    # loginUrl="http://10.25.25.117:8080/rest/auth/1/session"
    # response=requests.post(loginUrl,data=requestBody,headers={
    #     "Content-Type":"application/json"
    # })
    # print(response)
    # print(response.data)
